package com.erp.common.db.impl;

import com.erp.common.db.BaseMapper;
import com.erp.common.db.IService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class ServiceImpl <T,M extends BaseMapper> implements IService<T> {

    @Autowired
    private M baseMapper;


    @Override
    public int updateById(T t) {
        return baseMapper.updateById(t);
    }

    @Override
    public int deleteById(Serializable id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int insert(T t) {
        return baseMapper.insert(t);
    }

    @Override
    public PageInfo<T> getAll(Map map) {
        int pageNum = 1;
        int pageSize = 10;
        if (map != null) {
            if (null != map.get("pageNum")) {
                pageNum = Integer.parseInt(map.get("pageNum").toString());
            }

            if (null != map.get("pageSize")) {
                pageSize = Integer.parseInt(map.get("pageSize").toString());
            }
        }

        PageHelper.startPage(pageNum,pageSize);

        List list = baseMapper.getAll(map);

        return new PageInfo<T>(list);
    }

    @Override
    public T getById(Serializable id) {
        return (T) baseMapper.getById(id);
    }
}
